// pages/search/search.js
import { request } from "../../request/request"

Page({
  data: {
    goods: [],
    isFocus:false,
    inpValue:""
  },
  handleInput(e) {
    const { value } = e.detail;
    if (!value.trim()) {
      this.setData({
        isFocus:false
      })
      return;
    }
   this.setData({
    isFocus : true
   })
    // this.search(value);
    this.debouce(this.search(value), 500)

  },
  search(query) {
    request({ url: "/goods/search" }).then(res => {
      this.setData({
        goods: res.data.message.goods
      })
    })
  },
  // 防抖函数
  debouce(func, delay) {
    let timer = null
    return function (...args) {
      if (timer) clearTimeout(timer)
      timer = setTimeout(() => {
        func.apply(this, args)
      }, delay)
    }
  },
  handleCancel(){
    this.setData({
      goods:[],
      isFocus:false,
      inpValue:""
    })
  }

})